using UnityEngine;
using System.Collections;

public class KnifeBehaviour :Equipable
{
    const float c_KnifeDamage = 200;
    public KnifeBehaviour()
    {
        m_UseRange = 9.0f;
    }
    bool m_IsBeingUsed = false;
    Actor m_Owner = null;
    Actor m_Target = null;
    Vector3 targetPos;
    public override void Use(Actor targetActor, Actor owner)
    {
        if (IsInRange(owner, targetActor))
        {
            DoDamage(targetActor, owner);
        }
        else
        {
            owner.MoveTo(targetActor.transform.position);
            m_IsBeingUsed = true;
            m_Owner = owner;
            m_Target = targetActor;
            targetPos = m_Target.transform.position;
            //Debug.Log("asd");
        }
    }
    public override void Use(Vector3 targetPos, Actor owner)
    {
        //owner.MoveTo(targetPos);
    }
    public override void Update()
    {
        if (m_IsBeingUsed)
        {
            if (IsInRange(m_Owner, m_Target))
            {
                DoDamage(m_Target, m_Owner);
            }
            if (targetPos != m_Target.transform.position)
            {
                targetPos = m_Target.transform.position;
                m_Owner.MoveTo(m_Target.transform.position);
                m_IsBeingUsed = true;
            }
        }
    }
    public override void OnUnequip()
    {
        CancelOrder();
    }
    public void CancelOrder()
    {
        m_IsBeingUsed = false;
    }
    bool IsInRange(Actor owner, Actor target)
    {
        if (Vector3.Distance(owner.transform.position, target.transform.position) <= m_UseRange)
        {
            return true;
        }
        return false;
    }
    void DoDamage(Actor target, Actor owner)
    {
        owner.StopMoving();
        m_IsBeingUsed = false;
        target.OnHit(c_KnifeDamage, owner);
        owner.m_Animator.PlayKnifeAnimation();
        SoundManager.Instance.PlaySound("Knife", target.transform.position);
    }
}
